package com.serwer.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.border.TitledBorder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;



import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Archiwum extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTable table_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Archiwum frame = new Archiwum();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Archiwum() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 519, 330);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 45, 369, 196);
		contentPane.add(scrollPane_1);
		
		table = new JTable();
		table.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
			      DefaultTableModel model=(DefaultTableModel) table.getModel();
			        model.setRowCount(0);
			        String sql="select * from notow as n join indexy as i using(id_index)";          
			        try {			            
			            Class.forName("com.mysql.jdbc.Driver");
			            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			            Statement stmt=con.createStatement();
			            ResultSet rs = stmt.executeQuery(sql);
			            while (rs.next())
			            {
			               
			                String data= rs.getString("data");
			                String wart= rs.getString("wartosc");
			                String nazwa= rs.getString("index_name");

			                model.addRow(new Object []{
			                    data, wart, nazwa
			                });			    
			            }
			        }
			        catch(Exception e) {
			           // JOptionPane.showMessageDialog(this, e.getMessage());
			        }
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Data", "Index", "Warto\u015B\u0107", "+/-"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, true, true, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(95);
		table.getColumnModel().getColumn(1).setPreferredWidth(86);
		table.getColumnModel().getColumn(2).setPreferredWidth(87);
		table.getColumnModel().getColumn(3).setPreferredWidth(60);
		scrollPane_1.setViewportView(table);
		
		JLabel lblMserwer = new JLabel("mSerwer");
		lblMserwer.setForeground(new Color(0, 128, 128));
		lblMserwer.setFont(new Font("Tahoma", Font.BOLD, 26));
		lblMserwer.setBounds(327, 11, 116, 23);
		contentPane.add(lblMserwer);
		
		JLabel lblArchiwumNotowa = new JLabel("Archiwum notowa\u0144");
		lblArchiwumNotowa.setForeground(SystemColor.textHighlight);
		lblArchiwumNotowa.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblArchiwumNotowa.setBounds(10, 17, 147, 19);
		contentPane.add(lblArchiwumNotowa);
		
		JLabel lblNewLabel = new JLabel("Zalogowany jako Administrator ");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(10, 270, 162, 14);
		contentPane.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("Wszystkie");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DefaultTableModel model=(DefaultTableModel) table.getModel();
		        model.setRowCount(0);
		        String sql="select * from notow as n join indexy as i using(id_index)";          
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		               
		                String data= rs.getString("data");
		                String wart= rs.getString("wartosc");
		                String nazwa= rs.getString("index_name");

		                model.addRow(new Object []{
		                    data, wart, nazwa
		                });			    
		            }
		        }
		        catch(Exception e) {
		           // JOptionPane.showMessageDialog(this, e.getMessage());
		        }
				
		      
			}
		});
		btnNewButton.setBounds(389, 218, 100, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_4 = new JButton("Zamknij");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnNewButton_4.setBounds(389, 266, 100, 23);
		contentPane.add(btnNewButton_4);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(389, 45, 100, 162);
		contentPane.add(scrollPane);
		
		table_1 = new JTable();
		table_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int row = table_1.getSelectedRow();
	            String Table_click_id = (table_1.getModel().getValueAt(row, 0).toString());
	            System.out.println(Table_click_id);
	            DefaultTableModel model=(DefaultTableModel) table.getModel();
		        model.setRowCount(0);
		        String sql="select * from notow as n join indexy as i using(id_index) where i.id_index ="+quotate(Table_click_id)+"";          
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		               
		                String data= rs.getString("data");
		                String wart= rs.getString("wartosc");
		                String nazwa= rs.getString("index_name");

		                model.addRow(new Object []{
		                    data, wart, nazwa
		                });			    
		            }
		        }
		        catch(Exception ee) {
		           // JOptionPane.showMessageDialog(this, e.getMessage());
		        }
			}
		});
	
		table_1.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
				DefaultTableModel model=(DefaultTableModel) table_1.getModel();
		        //czyszczenie
		        model.setRowCount(0);
		        String sql="SELECT * FROM Indexy";			                 
		        try {
		            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");		           
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);

		            while (rs.next())
		            {
		               String id = rs.getString("id_index");
		                String nazwa= rs.getString("index_name");

		                model.addRow(new Object []{
		                   id, nazwa
		                });			    
		            }
		        }
		        catch(Exception e) {
		         //   JOptionPane.showMessageDialog(this, e.getMessage());
		        }
			}
		});
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "Index"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table_1.getColumnModel().getColumn(0).setResizable(false);
		table_1.getColumnModel().getColumn(0).setPreferredWidth(40);
		table_1.getColumnModel().getColumn(1).setResizable(false);
		scrollPane.setViewportView(table_1);
	}
	public String quotate(String content){
	    
        return "'"+content+"'";
    }
}
